//
//  HJPhotoItem.m
//  KaiChat
//
//  Created by lx on 2023/10/30.
//

#import "HJPhotoItem.h"
#import "HJChatMessageModel.h"

@implementation HJPhotoItem

+ (HJPhotoItem *)itemWithBlock:(void (^)(HJPhotoItem *item))block {
    HJPhotoItem *item = [[HJPhotoItem alloc] init];
    if (block) block(item);
    return item;
}

+ (HJPhotoItem *)itemWithMessageModel:(HJChatMessageModel *)model {
    HJPhotoItem *item = [[HJPhotoItem alloc] init];
    item.thumbUrl = model.thumbnailUrl;
    item.originalUrl = model.fileUrl;
    if (model.messageType == HJMessageTypeImage) {
        item.localPath = [HJMessageFileManager imagefileFilePathWithSessionId:model.sessionId fileName:model.localPath.lastPathComponent];
    } else {
        item.localPath = [HJMessageFileManager videofileFilePathWithSessionId:model.sessionId fileName:model.localPath.lastPathComponent];
    }
    return item;
}

- (void)loadData {
    if (self.preloadFinish) {
        return;
    }
    NSString *imageUrl = self.originalUrl ?: self.thumbUrl;
    [[SDWebImageManager sharedManager] loadImageWithURL:imageUrl.URL options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (image) {
            self.preloadFinish = YES;
        }
    }];
   
}

@end
